/**
 * Project Name:Android_Car_Example
 * File Name:RegeocodeTask.java
 * Package Name:com.amap.api.car.example
 * Date:2015年4月2日下午6:24:53
 */

package com.gears_software.sxdc.dccx.ui.map;

import android.content.Context;

import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.GeocodeSearch.OnGeocodeSearchListener;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.amap.api.services.geocoder.StreetNumber;
import com.gears_software.sxdc.dccx.data.model.PositionEntity;

/**
 * ClassName:RegeocodeTask <br/>
 * Function: 简单的封装的逆地理编码功能 <br/>
 * Date: 2015年4月2日 下午6:24:53 <br/>
 *
 * @author yiyi.qi
 * @see
 * @since JDK 1.6
 */
public class RegeocodeTask implements OnGeocodeSearchListener {
    private static final float SEARCH_RADIUS = 50;
    private OnLocationGetListener mOnLocationGetListener;

    private GeocodeSearch mGeocodeSearch;

    public RegeocodeTask(Context context) {
        mGeocodeSearch = new GeocodeSearch(context);
        mGeocodeSearch.setOnGeocodeSearchListener(this);
    }

    public void search(double latitude, double longitude) {
        RegeocodeQuery regecodeQuery = new RegeocodeQuery(new LatLonPoint(
                latitude, longitude), SEARCH_RADIUS, GeocodeSearch.AMAP);
        mGeocodeSearch.getFromLocationAsyn(regecodeQuery);
    }

    public void setOnLocationGetListener(
            OnLocationGetListener onLocationGetListener) {
        mOnLocationGetListener = onLocationGetListener;
    }

    @Override
    public void onGeocodeSearched(GeocodeResult arg0, int arg1) {

    }

    @Override
    public void onRegeocodeSearched(RegeocodeResult regeocodeReult,
                                    int resultCode) {
        if (resultCode == AMapException.CODE_AMAP_SUCCESS) {
            if (regeocodeReult != null
                    && regeocodeReult.getRegeocodeAddress() != null
                    && mOnLocationGetListener != null) {
                String address = regeocodeReult.getRegeocodeAddress()
                        .getFormatAddress();
                String city = regeocodeReult.getRegeocodeAddress().getCity();
                String province = regeocodeReult.getRegeocodeAddress().getProvince();
                String district = regeocodeReult.getRegeocodeAddress().getDistrict();
                StreetNumber streetNumber  = regeocodeReult.getRegeocodeAddress().getStreetNumber();
                String street = streetNumber.getStreet();
                String sn = streetNumber.getNumber();
                String cityCode = regeocodeReult.getRegeocodeAddress().getCityCode();
                String adCode = regeocodeReult.getRegeocodeAddress().getAdCode();
                PositionEntity entity = new PositionEntity();
                entity.setAddress(address);
                entity.setCity(city);
                entity.setProvince(province);
                entity.setDistrict(district);
                entity.setStreet(street);
                entity.setStreetNum(sn);
                entity.setCityCode(cityCode);
                entity.setAdCode(adCode);
                mOnLocationGetListener.onRegecodeGet(entity);

            }
        }
        //TODO 可以根据app自身需求对查询错误情况进行相应的提示或者逻辑处理
    }

}
